<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../../assets/img/tou_xiang.png" alt="管理平台头像">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠按钮区 -->
        <div class="toggle-button" @click="toggleCollapse">{{toggleButton}}</div>
        <!-- 侧边栏菜单 -->
        <el-menu background-color="#333744" text-color="#fff"
                 active-text-color="#409eff" :default-active="isActive"
                :collapse="isCollapse" :collapse-transition="false" unique-opened router>
          <!-- :unique-opened="true" 可以简写成 unique-opened -->
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的选项模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path"
                          :route="{ path: '/' + subItem.path, query: { bread : subItem.authName } }"
                          @click="toActive('/' + subItem.path)" 
                          v-for="subItem in item.children" :key="subItem.id">
              <!-- 二级菜单选项模板 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenu } from '@/api/home.js';


export default {
  data() {
    return {
      // toggle-button 折叠按钮
      toggleButton: '◀◀',
      isCollapse: false,
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        '125': 'iconfont icon-menu_xqrkgl',
        '103': 'iconfont icon-menu_yh_czqx',
        '101': 'iconfont icon-menu_spsj',
        '102': 'iconfont icon-menu_bmlb',
        '145': 'iconfont icon-chart-area',
      },
      // 高亮选项
      isActive: ''
    }
  },
  created() {
    this.getMenuList();
    this.isActive = window.sessionStorage.getItem('acticePath');
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    async getMenuList() {
      try {
        // 捕获错误，遇到错误，中断下面输出语句的执行，直接走catch执行
        const { data: res } = await getMenu(); 
        this.menulist = res;
        // console.log(this.menulist);
      } catch (err) {
        console.log(err);
      }
    },
    // 点击折叠按钮 实现侧边导航折叠
    toggleCollapse(){
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse) {
        this.toggleButton = '▶▶';
        return ;
      }
      this.toggleButton = '◀◀';
    },
    // 高亮，将index存入sessionStorage
    toActive(acticePath){
      window.sessionStorage.setItem('acticePath', acticePath);
      this.isActive = acticePath;
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 10px;
  align-items: center;
  color: #fff;
  font-size: 20px;

  >div{
    display: flex;
    align-items: center;
    span{
      margin-left: 15px;
    }
  }

  img{
    height: 50px;
    background-color: #fff;
    border-radius: 50%;
    }

}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
  font-size: 20px;
}

.toggle-button{
  background-color: #4a5064;
  color: #fff;
  font-size: 18px;
  line-height: 30px;
  text-align: center;
  letter-spacing: -0.3em;
  cursor: pointer;
}
</style>